package com.github.ilioili.justdoit.components.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.CardView;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.github.ilioili.justdoit.R;
import com.github.ilioili.justdoit.common.AppBaseActivity;
import com.github.ilioili.justdoit.common.config.AnimationConfig;
import com.github.ilioili.justdoit.common.config.Colors;
import com.github.ilioili.justdoit.widget.CircleAnimationFrame;
import com.taihe.template.base.injection.Id;
import com.taihe.template.base.injection.Layout;

/**
 * Created by hantuo on 16/5/23.
 */
@Layout(R.layout.dialog_pick_theme)
public class PickColorActivity extends AppBaseActivity {

    @Id(R.id.recyclerView)
    private RecyclerView recyclerView;
    @Id(R.id.circle_animation_frame)
    private CircleAnimationFrame circleAnimationFrame;

    private static OnColorSelectListener onColorSelectListener;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        recyclerView.setLayoutManager(new GridLayoutManager(this, 5));
        recyclerView.setAdapter(new RecyclerView.Adapter<Holder>() {
            @Override
            public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
                return new Holder(LayoutInflater.from(PickColorActivity.this).inflate(R.layout.theme_color_item, parent, false));
            }

            @Override
            public void onBindViewHolder(Holder holder, final int position) {
                holder.cardView.setCardBackgroundColor(Colors.ARRAY[position]);
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        circleAnimationFrame.collapse(CircleAnimationFrame.Gravity.Center, AnimationConfig.MEDIUM, true, new CircleAnimationFrame.CompleteListener() {
                            @Override
                            public void onComplete() {
                                onColorSelectListener.onSelect(Colors.ARRAY[position]);
                                finish();
                            }
                        });
                    }
                });
            }

            @Override
            public int getItemCount() {
                return Colors.ARRAY.length;
            }
        });
        circleAnimationFrame.expand(CircleAnimationFrame.Gravity.Center, AnimationConfig.MEDIUM);
    }


    public interface OnColorSelectListener {
        void onSelect(int color);
    }

    public static Intent newIntent(Context context, OnColorSelectListener listener) {
        Intent intent = new Intent(context, PickColorActivity.class);
        onColorSelectListener = listener;
        return intent;
    }

    @Override
    public void onBackPressed() {
        circleAnimationFrame.collapse(CircleAnimationFrame.Gravity.Center, AnimationConfig.MEDIUM, true, new CircleAnimationFrame.CompleteListener() {
            @Override
            public void onComplete() {
                finish();
            }
        });
    }

    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(0, 0);
    }

    class Holder extends RecyclerView.ViewHolder {
        CardView cardView;

        public Holder(View itemView) {
            super(itemView);
            cardView = (CardView) itemView.findViewById(R.id.cardView);
        }
    }
}
